package org.glut.cloud.authentication.config;

import org.springframework.cloud.gateway.filter.factory.GatewayFilterFactory;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import reactor.core.publisher.Mono;

@Configuration
public class GatewayConfiguration {

    /**
     * 接口限流：初始化接口路径作为限流key
     * @return
     */
    @Bean //Bean Name: apiKeyResolver
    KeyResolver apiKeyResolver() {
        System.err.println("\n=============================>\napiKeyResolver\n==============================");
        return exchange -> Mono.just(exchange.getRequest().getPath().value());
    }

    @Bean
    public RedisRateLimiterGatewayFilterFactory redisRateLimiterGatewayFilterFactory(RedisTemplate<String, String> redisTemplate) {
        System.err.println("\n=============================>\nredisRateLimiterGatewayFilterFactory\n==============================");
        return new RedisRateLimiterGatewayFilterFactory(redisTemplate);
    }

    @Bean
    public GatewayFilterFactory<RedisRateLimiterGatewayFilterFactory.Config> redisRateLimiterGatewayFilterFactoryBean(RedisRateLimiterGatewayFilterFactory factory) {
        return factory;
    }
}
